package cn.wh.pet.service.impl;

import cn.wh.pet.domain.OrderPetAcquisition;
import cn.wh.pet.domain.Pet;
import cn.wh.pet.domain.SearchMasterMsg;
import cn.wh.pet.dto.OrderHandleDto;
import cn.wh.pet.mapper.OrderPetAcquisitionMapper;
import cn.wh.pet.mapper.PetMapper;
import cn.wh.pet.mapper.SearchMasterMsgMapper;
import cn.wh.pet.service.IOrderPetAcquisitionService;
import cn.wh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-06-28
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    /**
     * 确认订单
     * @param dto
     */
    @Override
    public void confirm(OrderHandleDto dto) {
        //查询订单信息
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(dto.getId());
        //1.信息补充 订单、宠物
        order.setPaytype(dto.getPayType());
        order.setPrice(dto.getMoney());
        order.setState(1);
        orderPetAcquisitionMapper.update(order);
        Pet pet = petMapper.loadById(order.getPetId());
        pet.setCostprice(dto.getMoney());//成本价
        petMapper.update(pet);
        //2.各种修改   订单状态、寻主消息状态
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void cancel(Long id) {
        //查询订单
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //将订单状态设为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
        //2.寻主消息shopId置空--还会寻主池
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);
        //3.删除宠物
        petMapper.deleteById(order.getPetId());

    }
}
